using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Bll;

namespace IGlobalist.Cms.Common
{
    public class IGlobalistMembership
    {      
        
        public static bool IsAuthenticate
        {
            get 
            {
                return !(GetUser() == null);
                
            }
        }

        public static IUser GetUser(){
            try
            {
                IUser user=(IUser)HttpContext.Current.Session[SessionVariable.User.CurrentUserLoggedOn];
                if (user==null && 1==2){
                    HttpCookie cookie= HttpContext.Current.Request.Cookies.Get("currentUserID");
                    if (cookie!=null){
                        long userID=Convert.ToInt64( cookie.Value);
                        user=UserController.GetUser(userID);
                        HttpContext.Current.Session[SessionVariable.User.CurrentUserLoggedOn] = user;
                    }
                }

                

                return user;
            }
            catch
            {                
                return null;
            }
        }

        public static IUser GetUser(long id)
        {
            try
            {
                IUser  user = UserController.GetUser(id);
                   
                return user;
            }
            catch
            {
                return null;
            }
        }

        public static string Fullname
        {
            get{
                IUser user = GetUser();
                if (!String.IsNullOrEmpty(user.Firstname))
                {
                    return String.Format("{0} {1} {2}", user.Firstname, user.Middle, user.Lastname);
                }
                else
                {
                    return user.UserName;
                }
            }
        }

        public static IUser GetUser(string username, long classID)
        {
            try
            {
                return UserController.GetUser(username,classID);
            }
            catch
            {
                return null;
            }
        }

        
         
        public static bool IsInRole(BuiltinRole name)
        {            
            IRole role = RoleController.GetRole(name.ToString());
            bool isFound = false;
            IUser user=GetUser();

            if (role != null && user!=null && user.Roles!=null)
            {
                int i = 0;
                while (i < user.Roles.Length && !isFound)
                {
                    if (role.ID==Convert.ToInt64(user.Roles[i]))
                    {
                        isFound = true;
                    }
                    else
                    {
                        i++;
                    }
                }    
            
                
            }

            return isFound;
        }


        public static bool ValidateUser(string username, string password)
        {
            try
            {
                IList<ICmsClass> list = ClassController.GetClassListByClassTypeID((short)CmsObjectType.User);

                IUser user = UserController.GetUser(username, list[0].ID);
                if (user.Password.Equals(password))
                {
                    HttpContext.Current.Session[SessionVariable.User.CurrentUserLoggedOn] = user;
                    HttpCookie userLogin = new HttpCookie("currentUserID", user.ID.ToString());
                    
                    HttpContext.Current.Response.Cookies.Add(userLogin);
                    HttpContext.Current.Response.Redirect("Default.aspx",true);
                    return true;
                }

                
            }
            catch
            {

            }
            return false;
        }

        public static bool ValidateUserSite(string username, string password)
        {
            try
            {
                IList<ICmsClass> list = ClassController.GetClassListByClassTypeID((short)CmsObjectType.User);

                IUser user = UserController.GetUser(username, list[0].ID);
                if (user.Password.Equals(password))
                {
                    HttpContext.Current.Session[SessionVariable.User.CurrentUserLoggedOn] = user;
                    HttpCookie userLogin = new HttpCookie("currentUserID", user.ID.ToString());
                    HttpContext.Current.Response.Cookies.Add(userLogin);
                    return true;
                }


            }
            catch
            {

            }
            return false;
        }

        public static bool ValidateUser(string username, string password, long classID)
        {
            try
            {
                IUser user= UserController.GetUser(username, classID);
                if (user.Password.Equals(password))
                {
                    
                    HttpContext.Current.Session[SessionVariable.User.CurrentUserLoggedOn] = user;
                    HttpCookie userLogin = new HttpCookie("currentUserID", user.ID.ToString());

                    HttpContext.Current.Response.Cookies.Add(userLogin);
                    return true;
                }
            }
            catch
            {
                
            }
            return false;
        }

        public static bool Logout()
        {
            try
            {
                HttpContext.Current.Response.Cookies.Remove("currentUserID");
                HttpContext.Current.Response.Cookies.Clear();
                HttpContext.Current.Session.Abandon();

                return true;
            }
            catch
            {

            }
            return false;
        }
    }
}
